/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import Entity.Categories;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author THANHDAT
 */
public class DAO_Categories {

    public DAO_Categories() {
    }

    public static List<Categories> showCategory() {
        List<Categories> lstCate = new ArrayList<Categories>();
        String query = "select * from tblCategories";
        try {
            Statement st = DAO_Connect.getConnection().createStatement();
            ResultSet rs = st.executeQuery(query);
            while (rs.next()) {
                int id = rs.getInt(1);
                String name = rs.getString(2);
                Categories cate = new Categories(id, name);
                lstCate.add(cate);
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return lstCate;
    }

    public Categories getId(int id) {
        Categories cate = null;
        try {
            
            String query = "select * from tblCategories where catId = " + id;
            Statement st = DAO_Connect.getConnection().createStatement();
            ResultSet rs = st.executeQuery(query);
            while (rs.next()) {
                int cid = rs.getInt(1);
                String name = rs.getString(2);  
                cate.setId(cid);
                cate.setName(name);
            }
        } catch (SQLException ex) {
            Logger.getLogger(DAO_Categories.class.getName()).log(Level.SEVERE, null, ex);
        }
        return cate;
    }
}
